﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// 表面模式
/// 所有的系统由此管理
/// 单例模式
/// 在面板显示
/// </summary>
public class Facade : MonoBehaviour {
    public static Facade Instance;
    private Model[] models;
    [Header("systems"),SerializeField]
    private ObjPool effects;
    [SerializeField]
    private PlayerController _playerController;
    public ObjPool Effects
    {
        get
        {
            return effects;
        }
    }

    public PlayerController PlayerController
    {
        get
        {
            return _playerController;
        }
    }


    #region mono

    private void Awake()
    {
        Instance = this;
        DontDestroyOnLoad(gameObject);
        models = new Model[] { effects };//write me 把各个系统填充进来
        _playerController = FindObjectOfType<PlayerController>();
        foreach (var m in models)
        {
            m.Init();
        }

    }

    private void Start()
    {
        foreach (var m in models)
        {
            m.OnStart();
        }
    }

    private void Update()
    {
        foreach (var m in models)
        {
            m.OnUpdata();
        }
    }


    #endregion


    /// <summary>
    /// 得到鼠标在主角平面的点
    /// </summary>
    /// <returns></returns>
    public static Vector3 GetMousePoint()
    {

        //从摄像机发出到点击坐标的射线
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        //Debug.Log(Input.mousePosition);
        RaycastHit[] hitInfo= Physics.RaycastAll(ray, 100f);
        foreach (var hit in hitInfo)
        {
            //划出射线，只有在scene视图中才能看到
            //Debug.DrawLine(ray.origin, hitInfo.point);
            GameObject gameObj = hit.collider.gameObject;
            //Debug.Log("click object name is " + gameObj.name);

            if(gameObj.layer==ConstField.MOUSE_LAYER)
            return hit.point;
        }
        return Vector3.zero;
    }

}
[System.Serializable]
public abstract class Model
{
    public virtual void Init() { }
    public virtual void OnStart() { }
    public virtual void OnUpdata() { }
    public virtual void Unload() { }
}
